home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
- #include <commdlg.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <alloc.h>
- #include <ctype.h>
- #include <math.h>
- #include <shellapi.h>
- #include <dos.h>
- #include <fcntl.h>
- #include <new.h>
- #include <setjmp.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <time.h>
- #ifdef __WIN32__
- #define huge far
- #define hmemcpy memcpy
- #pragma option -w-sig
- #endif
- #define reg register
- #define uns unsigned
- #define OUCH MB_ICONEXCLAMATION
- #define FM(type,n) (type far*)farmalloc((long)(sizeof(type))*n)
- #define HM(type,n) (type huge*)farmalloc((long)(sizeof(type))*n)
- #include "typecase.h"
- #pragma hdrstop
- /*-----*/
- #define swop(i,j) ((i)^=(j),(j)^=(i),(i)^=(j))
- #define reg register
- #define uns unsigned
- typedef uns char byte;
- typedef uns long ul;
- typedef uns short us;
- HINSTANCE Instance=0,PrevInstance=0; short CmdShow=0; HWND Main; HFONT xaf=0,yaf=0;
- CHOOSEFONT CF; LOGFONT LF; byte KEYS[256],buffer[256]; char tit[256],file[256]; OPENFILENAME ofn;
- char style[LF_FACESIZE]={0},font[LF_FACESIZE]={0};
- /*-----*/
- void Line(HDC dc,short x,short y,short X,short Y){MoveToEx(dc,x,y,0); LineTo(dc,X,Y);}
- /*-----*/
- void LC(char*s){for(;*s;s++) if(*s>='A') if(*s<='Z') *s+=32;}
- /*-----*/
- short GetFileName(HWND wn,char*file,char*t,char*f,short write) {short j;
- strcpy(file,tit);
- ofn.lStructSize=sizeof(OPENFILENAME); ofn.hwndOwner=wn;
- ofn.lpstrFilter=f; ofn.lpstrCustomFilter=0; ofn.nFilterIndex=0;
- ofn.lpstrFile=file; ofn.nMaxFile=128;
- ofn.lpstrFileTitle=tit; ofn.nMaxFileTitle=256;
- ofn.lpstrInitialDir=0; ofn.lpstrTitle=t;
- ofn.Flags=write?OFN_OVERWRITEPROMPT:OFN_FILEMUSTEXIST;
- j=(write?GetSaveFileName(&ofn):GetOpenFileName(&ofn))?1:0;
- LC(file); LC(tit); return j;}
- /*-----*///
- void textcursor(HDC dc,short x,short y,short h){
- Line(dc,x,y,x,y+h); Line(dc,x-5,y,x+5,y); Line(dc,x-5,y+h-1,x+5,y+h-1);}
- /*-----*/
- void choosefont(HWND wn,HDC dc){CF.lStructSize=sizeof(CF); CF.hwndOwner=wn; CF.hDC=dc;
- CF.lpLogFont=&LF; CF.Flags=CF_BOTH|CF_FORCEFONTEXIST|CF_USESTYLE; CF.lpszStyle=style;
- if(!ChooseFont(&CF)) font[0]=0; else strcpy(font,LF.lfFaceName);}
- /*-----*/
- LRESULT far pascal MainWinProc(HWND wn,UINT iMessage,WPARAM S,LPARAM L){HDC dc; PAINTSTRUCT PS;
- short i,j,k,l; RECT rr; static char p1616=1,I=16,J=16,sb=0,di,dj,Numbers=0,nbuf[4]; SIZE K;
- HFONT of,xf; WPARAM T; static POINT P; static RECT r; static short h,d1=-1,nb=0,nc=0,os=0;
- static void*numf=0; static char*filter="font files\000*.fon;*.fot;*.ttf\000\000";
- switch(iMessage) {default: DEF: return DefWindowProc(wn,iMessage,S,L);
- case WM_ENTERIDLE: return 0;
- case WM_CREATE: SetMenu(wn,LoadMenu(Instance,"TYPECASE")); ShowWindow(wn,SW_SHOW);
- if(!numf) numf=CreateFont(8,0,0,0,200,0,0,0,1,0,0,0,FF_MODERN,"System"); goto DEF;
- case WM_QUIT: if(numf) DeleteObject(numf); goto DEF;
- case WM_GETMINMAXINFO: {MINMAXINFO*p=(MINMAXINFO*)L;
- p->ptMaxPosition.x=p->ptMaxPosition.y=-4; return 0;}
- case WM_DESTROY: PostQuitMessage(0); return 0;
- case WM_PAINT: {GetClientRect(wn,&r);
- BeginPaint(wn,&PS); dc=PS.hdc; di=r.right/I; dj=r.bottom/(J+sb);
- xaf=CreateFont(100,0,0,0,LF.lfWeight,LF.lfItalic,0,0,1,0,0,0,FF_DONTCARE,font);
- of=SelectObject(dc,xaf);
- if(d1<0) {
- for(j=i=0;i<256;i++) {GetTextExtentPoint32(dc,(char*)&i,1,&K); k=K.cx; if(j<k) j=k;} j+=3;
- h=dj*.9; if(j>di) {i=(100*di)/j; if(h>i) h=i;}
- yaf=CreateFont(h,0,0,0,LF.lfWeight,LF.lfItalic,0,0,1,0,0,0,FF_DONTCARE,font);
- SelectObject(dc,yaf);
- for(j=0;j<J;j++) for(i=0;i<I;i++) {k=i+j*I; TextOut(dc,i*di+2,j*dj+2,(char*)&k,1);}
- if(Numbers) {xf=SelectObject(dc,numf); for(j=0;j<J;j++) for(i=0;i<I;i++) {k=i+j*I;
- sprintf(nbuf,Numbers-2?"%3d":" %02X",k);
- TextOut(dc,(i+1)*di+2-18,(j+1)*dj+2-+10,nbuf,3);}
- SelectObject(dc,xf);}
- for(j=1;j<=J;j++) Line(dc,0,j*dj,I*di,j*dj);
- for(i=1;i<=I;i++) Line(dc,i*di,0,i*di,J*dj);
- if(sb) {buffer[nb]=0; GetTextExtentPoint32(dc,(char*)buffer,nb,&K); k=K.cx;
- GetTextExtentPoint32(dc,(char*)buffer,nc,&K); P.x=K.cx; P.y=dj*J+2;
- if(P.x+os>r.right-20) os=r.right-20-P.x;
- if(P.x+os<20) os=20-P.x; if(os>0) os=0;
- TextOut(dc,os,dj*J+2,(char*)buffer,nb);
- textcursor(dc,P.x+os,P.y,h-1);}}
- else {
- GetTextExtentPoint32(dc,(char*)&d1,1,&K); j=K.cx+3;
- h=r.bottom*.9; if(j>r.right) {i=(100*r.right)/j; if(h>i) h=i;}
- yaf=CreateFont(h,0,0,0,LF.lfWeight,LF.lfItalic,0,0,1,0,0,0,FF_DONTCARE,font);
- SelectObject(dc,yaf); GetTextExtentPoint32(dc,(char*)&d1,1,&K); j=K.cx;
- k=(r.right-j)/2; if(k<1) k=1; l=(r.bottom-h)/2; if(l<1) l=1;
- TextOut(dc,k,l,(char*)&d1,1);
- Line(dc,k,l-1,k+j,l-1); Line(dc,k ,l-1,k ,l+h);
- Line(dc,k,l+h,k+j,l+h); Line(dc,k+j,l-1,k+j,l+h); d1=-1;}
- SelectObject(dc,of); DeleteObject(xaf); DeleteObject(yaf); EndPaint(wn,&PS); return 0;}
- case WM_KEYDOWN: case WM_SYSKEYDOWN: case WM_KEYUP: case WM_SYSKEYUP: {byte V=(byte)(S&0xff);
- uns short /*rep=(short)(L&0xffff),*/ scan=(short)((L>>16)&0xff), /*ext=(short)((L>>24)&1),*/
- /*alt=(short)((L>>29)&1),*/ up=iMessage&1, rel=(short)((L>>31)&1); uns short G;
- KEYS[V]=rel?0:0xff; G=0; if(S!=VK_BACK) j=ToAscii(S,scan,KEYS,&G,1); i=(byte)(G&0xff);
- if(i) if(!up) goto CHAR;
- if(!up) switch(V) {
- case VK_F1: T=_LAYOUT_; goto MENU;
- case VK_F2: T=_SHOWBUF_; goto MENU;
- case VK_F3: T=_NUMBERS_; goto MENU;
- case VK_LEFT: if(nc) nc--; goto CC;
- case VK_RIGHT: if(nc<256) if(nc<nb) nc++; goto CC;
- case VK_BACK: if(!nc) break; nc--; /* run on */
- case VK_DELETE: if(nc==nb) break; for(i=nc+1;i<nb;i++) buffer[i-1]=buffer[i]; nb--; goto CC;
- case VK_END: nc=nb; goto CC;
- case VK_HOME: nc=0; goto CC;}}
- case WM_MOUSEMOVE: case WM_LBUTTONDOWN: break;
- case WM_RBUTTONDOWN: i=((short)L)/di; j=((short)(L>>16))/dj; d1=(i+j*I)&255; /* run on */
- case WM_RBUTTONUP: InvalidateRect(wn,0,1); goto CC;
- case WM_LBUTTONUP: {i=((short)L)/di; j=((short)(L>>16))/dj; i=i+j*I; if(!i) break;
- CHAR: if(!i ?1: nb>255) break; for(l=nb;l>nc;l--) buffer[l]=buffer[l-1];
- buffer[nc++]=(byte)i; nb++;
- CC: SetRect(&rr,0,J*dj,r.right,r.bottom); InvalidateRect(wn,&rr,1);} goto DEF;
- case WM_COMMAND: T=S; goto MENU;}
- goto DEF;
- MENU: switch((unsigned short)T) {
- default: goto DEF;
- case _HELP_: WinHelp(wn,"TYPECASE.HLP",HELP_CONTENTS,0); break;
- case _FONT_: FONT: {char X[128]; dc=GetDC(wn); choosefont(wn,dc);
- ReleaseDC(wn,dc); AUTOREAD: sprintf(X,"Typecase: %s, %s",LF.lfFaceName,CF.lpszStyle);
- SendMessage(wn,WM_SETTEXT,0,(long)X); IR: InvalidateRect(wn,0,1); goto DEF;}
- case _LAYOUT_: p1616=1-p1616; LAYOUT: if(p1616) I=J=16; else {I=32; J=8;} goto IR;
- case _SHOWBUF_: sb=1-sb; goto IR;
- case _TOCLIP_: {HGLOBAL g;
- if(!OpenClipboard(wn)) {MessageBox(wn,"I can't open the clipboard","",OUCH); break;}
- EmptyClipboard(); g=GlobalAlloc(2,nb+1); lstrcpy((char*)GlobalLock(g),(char*)buffer);
- GlobalUnlock(g); SetClipboardData(CF_TEXT,g); CloseClipboard(); break;}
- case _READFONT_: if(!GetFileName(wn,file,"font file?",filter,0)) goto DEF;
- if(!AddFontResource(file)) MessageBox(wn,file,"This Windows can't read a font from",OUCH);
- else SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
- {char fot[512]; strcpy(fot,file); j=strlen(fot); FILE*FOT;
- if(j<4 ?1: stricmp(fot+j-4,".TTF")) goto DEF; strcpy(fot+j-3,"FOT");
- FOT=fopen(fot,"r"); if(FOT) {fclose(FOT); goto DEF;}
- if(MessageBox(wn,"Its corresponding .FOT file doesn't exist: shall I create it?",file,
- MB_YESNO)==IDYES) CreateScalableFontResource(0,fot,file,0);}
- goto DEF;
- case _DROPFONT_: if(!GetFileName(wn,file,"font file?",filter,0)) goto DEF;
- if(!RemoveFontResource(file))
- MessageBox(wn,file,"I haven't got a font read from",OUCH);
- else SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0); goto DEF;
- case _NUMBERS_: Numbers++; if(Numbers>2) Numbers=0; goto IR;
- case _TOCB_: {RECT R; GetClientRect(wn,&R); HDC dc=GetDC(wn),ndc=CreateCompatibleDC(dc);
- HBITMAP bm=CreateCompatibleBitmap(dc,R.right,R.bottom);
- HBITMAP obm=SelectObject(ndc,bm);
- BitBlt(ndc,0,0,R.right,R.bottom,dc,0,0,SRCCOPY);
- if(OpenClipboard(wn)) {EmptyClipboard(); SetClipboardData(CF_BITMAP,bm);} CloseClipboard();
- SelectObject(ndc,obm); ReleaseDC(wn,dc); goto DEF;}
- case _EXIT_: SendMessage(wn,WM_CLOSE,0,0); return 0;}
- return 0;}
- /*-----*/
- static void Register(char*name,WNDPROC proc) {WNDCLASS c;
- c.style=CS_HREDRAW|CS_VREDRAW; c.lpfnWndProc=proc; c.cbClsExtra=0;
- c.cbWndExtra=2; c.hInstance=Instance; c.hIcon=LoadIcon(Instance,"TYPECASE");
- c.hCursor=LoadCursor(0,IDC_ARROW); c.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
- c.lpszMenuName=0; c.lpszClassName=name; if(!RegisterClass(&c)) exit(0);}
- /*-----*/
- HWND newwindow(char*clas,char*name,long c=0){
- HWND w=CreateWindow(clas,name,WS_OVERLAPPEDWINDOW|c,
- CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,Instance,0);
- if(w) {ShowWindow(w,SW_SHOW); UpdateWindow(w);} return w;}
- /*-----*/
- char*progname="Typecase";
- // Turn off warning: Parameter 'CmdLine' is never used in function WinMain(unsigned short,unsigned short,char far*,short)
- #pragma argsused
- // Turn off warning: 'MainWnd' is assigned a value that is never used in function WinMain(unsigned short,unsigned short,char far*,short)
- #pragma option -w-aus
- /*-----*/
- int PASCAL WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int nCmdShow){
- short i; MSG msg; Instance=hInst; PrevInstance=hPrev; CmdShow=(short)nCmdShow;
- if(!hPrev) Register(progname,MainWinProc); for(i=0;i<256;i++) KEYS[i]=0;
- Main=newwindow(progname,progname,/*WS_HSCROLL|WS_VSCROLL|*/WS_MAXIMIZE); if(!Main) exit(0);
- while(GetMessage(&msg,0,0,0)) {TranslateMessage(&msg); DispatchMessage(&msg);}
- return msg.wParam;}
-